WebWork 2 : WebWork Freemarker Support
This page last changed on Nov 30, 2004 by jcarreira.
Freemarker Support in WebWorkFreemarker views can be rendered either via the webwork result type freemarker, or by using the dispatcher result type in conjunction Webwork's FreemarkerServlet.This document will focus on using the freemarker result since it is the recommended approach. An section follows to show how to use the FreemarkerServlet. Configure your action to use the freemarker result typeThe freemarker result type is defined in webwork-default.xml, so normally you just include it, and define your resutls to use type="freemarker". <include file="webwork-default.xml"/> ... <action name="test" class="package.Test"> <result name="success" type="freemarker">/WEB-INF/views/testView.ftl</result> </action> ... Property ResoloutionYour action properties are automatically resolved - just like in a velocity view.for example ${name} will result in stack.findValue("name"), which generaly results in action.getName() being executed. A search process is used to resolve the variable, searching the following scopes in order, until a value is found :
Objects in the ContextThe following variables exist in the freemarer views
FreeMarker configuration with recent (post 2.1) releasesTo configure the freemarker engine that webwork uses, just add a file freemarker.properties to the classpath. The supported properties are those that the freemarker Configuration object expects - see the freemarker documentation for these. These properties are used for both the freemarker result type, and the webwork provided FreemarkerServlet.default_encoding=ISO-8859-1 template_update_delay=5 locale=no_NO Using webwork UI tags - or any JSP Tag LibraryFreemarker has builtin support for using any JSP taglib. You can use JSP taglibs in FreeMarker even ifa) your servlet container has no support for JSP, or b) you didn't specify the taglib in your web.xml - note how in the example below we refer to the taglib by its webapp-absolute URL, so no configuration in web.xml is needed. <#assign ww=JspTaglibs["/WEB-INF/webwork.tld"] /> <@ww.form method="'post'" name="'inputform'" action="'save.action'" > <@ww.hidden name="'id'" /> <@ww.textarea label="'Details'" name="'details'" rows=5 cols=40 /> <@ww.submit value="'Save'" align="center" /> </@ww.form> NOTE : numeric properties for tags MUST be numbers, not strings. as in the rows and cols properties above. if you use cols="40" you will receive an exception. Other than that, the freemarker tag container behaves as you would expect. Using the FreemarkerServletThe FreemarkerServlet provided in the freemarker.jar will work out of the box however it won't provide any webwork specific functionality such as the context variables, property resoloution etc. Therefore webwork provides its own servlet to provide this integration. Register the FreemarkerServlet in web.xmlTo use freemarker as a view engine, the webwork2 FreemarkerServlet needs to be configured, and mapped to the file extension that you use for your templates. <servlet> <servlet-name>freemarker</servlet-name> <servlet-class>com.opensymphony.webwork.views.freemarker.FreemarkerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> Configure Actions to use this servlet (xwork.xml configuration)To use the freemarker view, just use the dispatcher result type, and specify the location to the template file.<action name="test" class="package.Test"> <result name="success" type="dispatcher">/WEB-INF/views/testView.ftl</result> </action> Extending the servletNOTE: these docs need to be revised, since the FreemarkerServlet has changed since they were written.Please refer to the freemarker site for details about the base freemarker servlet. Be carfeul when subclassing com.opensymphony.webwork.views.freemarker.FreemarkerServlet when overridingprotected TemplateModel createModel(
ObjectWrapper wrapper,
ServletContext servletContext,
HttpServletRequest request,
HttpServletResponse response)
Please call super.createModel(...) and wrap it with a new model to avoid problems with action property resoloution. |
Document generated by Confluence on Dec 14, 2004 16:37 |